﻿Public Class CustomBufferPanel
    Inherits Panel
    Private _IsActive As Boolean = True
    Property IsActive() As Boolean
        Get
            Return _IsActive
        End Get
        Set(ByVal v As Boolean)
            _IsActive = v
            Invalidate()
        End Set
    End Property
    Private _DoubleBuffered As Boolean = True
    Property IsDoubleBuffered() As Boolean
        Get
            Return _DoubleBuffered
        End Get
        Set(ByVal v As Boolean)
            _DoubleBuffered = v
            If _DoubleBuffered = True Then
                SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
                DoubleBuffered = True
                UpdateStyles()
            Else
                SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
                DoubleBuffered = False
                UpdateStyles()
            End If
            Invalidate()
        End Set
    End Property
    Protected Overrides Sub OnPaint(pe As PaintEventArgs)
        If IsActive = True Then
            MyBase.OnPaint(pe)
        End If
    End Sub
    Public Sub New()
        SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
        UpdateStyles()
    End Sub
End Class